package com.yuchen.ishop.service.admin.buyer.impl;

import com.yuchen.core.MyPager;
import com.yuchen.core.jdbc.BaseDao;
import com.yuchen.ishop.entity.Buyer;
import com.yuchen.ishop.service.admin.buyer.BuyerService;
import org.apache.commons.lang.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.sql.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
public class BuyerServiceImpl implements BuyerService {

    @Autowired
    private BaseDao dao;

    //@Override
    //public List find() {
    //Condition c = Cnd.where("dicKey", "=", Gobals.SYS_GOODS_TYPE_KEY).asc("sort");
    //return dao.query(Buyer.class,null);
    // }

    @Override
    public MyPager find(Map params, MyPager pager) {
        Criteria cri = Cnd.cri();
        if (StringUtils.isNotBlank((String) params.get("account"))) {
            cri.where().andLike("account", params.get("account") + "%");
        }
        if (StringUtils.isNotBlank((String) params.get("state"))) {
            cri.where().andEquals("state", params.get("state"));
        }
        if (StringUtils.isNotBlank((String) params.get("identification"))) {
            cri.where().andEquals("identification", params.get("identification"));
        }
        cri.getOrderBy().desc("createTime");
        return dao.queryPager(Buyer.class, cri, pager);
    }

    @Override
    public Buyer insert(Buyer buyer) {

        return dao.insert(buyer);
    }

    @Override
    public int update(Buyer buyer, boolean isIgnoreNull) {
        if (isIgnoreNull) return dao.updateIgnoreNull(buyer);
        return dao.update(buyer);
    }

    @Override
    public Buyer getById(Long id) {
        return dao.fetch(Buyer.class, id);
    }

    @Override
    public int delete(Long id) {
        return dao.delete(Buyer.class, id);
    }


}
